package com.charmzenus.it.zuul.base;

import com.charmzenus.it.zuul.constants.CodeNumber;
import com.charmzenus.it.zuul.utils.JsonUtil;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;

public abstract class BaseZuulFilter extends ZuulFilter {

    protected void errorHandler(RequestContext requestContext, String errorMsg) {
        errorHandler(requestContext, CodeNumber.error_normal, errorMsg);
    }

    protected void errorHandler(RequestContext requestContext, int code, String errorMsg) {
        if (StringUtils.isEmpty(errorMsg)) errorMsg = "error";
        requestContext.setSendZuulResponse(false);
        requestContext.setResponseStatusCode(HttpStatus.OK.value());
        String json = JsonUtil.formatJson(code, null, errorMsg);
        requestContext.setResponseBody(json);
    }

}
